package com.mdxx.xczd.ui.presenter;

import com.google.gson.Gson;
import com.mdxx.xczd.api.ApiManage;
import com.mdxx.xczd.app.App;
import com.mdxx.xczd.app.ConstantsValue;
import com.mdxx.xczd.app.UrlValue;
import com.mdxx.xczd.bean.BarCodeBean;
import com.mdxx.xczd.bean.BarCodeBeanResult;
import com.mdxx.xczd.bean.BindStateResult;
import com.mdxx.xczd.ui.contract.FirstContract;
import com.mdxx.xczd.utils.SPUtil;
import com.mdxx.xczd.utils.ToastUtil;
import com.orhanobut.logger.Logger;

import rx.Observer;

/**
 * Created by MD-02 on 2017/2/6.
 */

public class FirstPresenter extends FirstContract.Presenter{
    @Override
    public void requestBarCode(final int type) {
        mModel.getBarCodeData()
                .subscribe(new Observer<BarCodeBeanResult>() {
                    @Override
                    public void onCompleted() { }

                    @Override
                    public void onError(Throwable e) {
                        Logger.d("requestBarCode"+e.toString());
                        ToastUtil.showShort("网络错误");
                    }

                    @Override
                    public void onNext(BarCodeBeanResult barCodeBeanResult) {
                        if (barCodeBeanResult.code == 0 || barCodeBeanResult.code == 3) {
                            String data = new Gson().toJson(barCodeBeanResult.data);
                            SPUtil.setSharedStringData("barcode", data);
                            SPUtil.setSharedIntData("first", 1);
                            SPUtil.setSharedIntData("isbind", barCodeBeanResult.code);
                            BarCodeBean barCodeBean = barCodeBeanResult.data;
                            App.barCodeBean = barCodeBean;
                            UrlValue.init(barCodeBean);
                            ConstantsValue.autoSize = barCodeBean.getAutoAlarmTime()/1000;
                            ConstantsValue.distance = barCodeBean.getAutoAlarmMetre();
                            ApiManage.getInstence().initApiService();
                            mView.returnBarCodeData(type, barCodeBeanResult.code);
                        } else if (barCodeBeanResult.code == 1) {
                            ToastUtil.showShort(barCodeBeanResult.errorMsg);
                        }
                    }
                });
    }

    @Override
    public void getBindState() {
        mModel.getBindState()
                .subscribe(new Observer<BindStateResult>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showShort("网络错误");
                    }

                    @Override
                    public void onNext(BindStateResult bindStateResult) {
                        if (bindStateResult.getCode() == 0) {
                            mView.returnBindState(bindStateResult.getData().getState());
                        }
                    }
                });

    }
}
